package com.example;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;

public class DetailedGraphicalDisplay extends WeatherDisplay {
    private JLabel iconLabel;
    private JLabel tempLabel;
    private JLabel detailsLabel;
    private ImageIcon weatherIcon;
    
    public DetailedGraphicalDisplay() {
        setLayout(new BorderLayout(10, 10));
        setBackground(new Color(240, 248, 255));
        
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        topPanel.setBackground(new Color(173, 216, 230));
        
        iconLabel = new JLabel("", JLabel.CENTER);
        iconLabel.setPreferredSize(new Dimension(100, 100));
        tempLabel = new JLabel("", JLabel.CENTER);
        tempLabel.setFont(new Font("微软雅黑", Font.BOLD, 36));
        
        topPanel.add(iconLabel);
        topPanel.add(tempLabel);
        
        detailsLabel = new JLabel("", JLabel.CENTER);
        detailsLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        add(topPanel, BorderLayout.NORTH);
        add(detailsLabel, BorderLayout.CENTER);
    }
    
    @Override
    public void updateWeather(WeatherData data) {
        try {
            URL url = new URL(data.getIconUrl());
            weatherIcon = new ImageIcon(ImageIO.read(url));
            iconLabel.setIcon(weatherIcon);
        } catch (Exception e) {
            iconLabel.setIcon(null);
            iconLabel.setText("[图标加载失败]");
        }
        
        tempLabel.setText(String.format("%.1f°C", data.getTemperature()));
        
        String html = String.format("<html><div style='text-align:center;'>" +
                "<h2>%s</h2>" +
                "<p>体感温度: %.1f°C</p>" +
                "<p>天气: %s</p>" +
                "<p>湿度: %d%%</p>" +
                "<p>风速: %.1f m/s</p></div></html>",
                data.getLocation(), data.getFeelsLike(), 
                data.getDescription(), data.getHumidity(), data.getWindSpeed());
        
        detailsLabel.setText(html);
    }
}